﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_KhachHangThanThiet_KHTT_ThanhToan : System.Web.UI.Page
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        if (gf.IsLogin() != 1)
        {
            Response.Redirect("/SMO/ChucNang/NguoiDung/NguoiDung_DangNhap.aspx");
        }
        if (gf.IsKHTT() != 1)
        {
            Response.Write("<script>alert('Bạn phải là khách hàng thân thiết mới thực hiện mua sản phẩm online!')</script>");
            Response.Redirect("/SMO/ChucNang/NguoiDung/NguoiDung_DangNhap.aspx");
        }
        id = gf.getLoginId();
        if (!this.IsPostBack)
        {
            DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
            DataDS.TAIKHOANDataTable tktable = tkadapter.TAIKHOAN_LayTheoId(id);
            txtDienThoai.Text = tktable.Rows[0]["DienThoai"].ToString();
            txtDiaChi.Text = tktable.Rows[0]["DiaChi"].ToString();
        }
      /*  NganHangService.Service sv = new NganHangService.Service();
        sv.Url = "http://localhost:1200/NganHang/Service.asmx";
        float a=sv.ThanhToanTien("123456", 20);
        Response.Write(a.ToString());*/
    }
    protected void btnThanhToan_Click(object sender, EventArgs e)
    {
        DataTable table = (DataTable)Session["GioHang"];
        float tong = 0;
        int i;
        for (i = 0; i < table.Rows.Count; i++)
        {
            if (table.Rows[i]["Is_KhuyenMai"].ToString() == "False")
                tong += float.Parse(table.Rows[i]["TongTien"].ToString());
        }
        if (tong != 0)
        {
            NganHang_WS.ThanhToanService sv = new NganHang_WS.ThanhToanService();
            float valuereturn= sv.ThanhToanTien(txtSoTaiKhoan.Text, "1111111111", tong);
            if (valuereturn >= 0)
            {
                DataDSTableAdapters.DATHANGTableAdapter dhadapter = new DataDSTableAdapters.DATHANGTableAdapter();
                int? kq = 0;
                dhadapter.DATHANG_ThemDonDatHang(id, txtDienThoai.Text, txtDiaChi.Text, DateTime.Now, null, null, tong, 4, null, ref kq);
                DataDSTableAdapters.CHITIET_DATHANGTableAdapter ctdhadapter = new DataDSTableAdapters.CHITIET_DATHANGTableAdapter();
                for (i = 0; i < table.Rows.Count; i++)
                {
                    int Id_SP = int.Parse(table.Rows[i]["Id_SP"].ToString());
                    int SoLuong = int.Parse(table.Rows[i]["SoLuong"].ToString());
                    float DonGia = float.Parse(table.Rows[i]["DonGia"].ToString());
                    float TongTien = float.Parse(table.Rows[i]["TongTien"].ToString());
                    Boolean Is_KhuyenMai = false;
                    if (table.Rows[i]["Is_KhuyenMai"].ToString() == "True")
                        Is_KhuyenMai = true;
                    ctdhadapter.CHITIET_DATHANG_Them(kq, Id_SP, SoLuong, DonGia, TongTien, Is_KhuyenMai);
                }
                table.Rows.Clear();
                Session["GioHang"] = table;
                Response.Write("<script>alert('Đã thanh toán thành công!');location.href='KHTT_XemThongTinDonDatHang.aspx';</script>");
            }
            else
            {
                if (valuereturn == -1)
                    Response.Write("<script>alert('Tài Khoản Không Tồn Tại!');</script>");
                if (valuereturn == -2)
                    Response.Write("<script>alert('Tài Khoản Đã Hết Hạn!');</script>");
                if (valuereturn == -3)
                    Response.Write("<script>alert('Không Đủ Tiền!');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Không có đơn hàng');</script>");
        }
    }
}
